"""
Case Type   : 故障&可靠性测试
Case Name   : 目录磁盘满时创建目录对象
Create At   : 2022/05/07
Owner       : li-xin12345
Description :
    1.创建目录
    2.使用cfe工具注入故障，使/tmp空间磁盘满
    3.初始用户创建目录对象
    4.清除故障
    5.删除目录
Expect      :
    1.创建目录成功
    2.注入故障成功
    3.创建目录对象成功
    4.清除故障成功
    5.删除目录成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class CreateDirectory(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.userNode = Node('PrimaryDbUser')
        self.primaryRootNode = Node('PrimaryRoot')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.dir_name = 'dir_object_create_directory_0001'
        self.dir_path = os.path.join('/tmp', 'dir_create_directory_0001')

    def test_create_directory(self):
        text = '-----step1:创建目录,expect:创建目录成功-----'
        self.logger.info(text)
        mkdir_cmd = f"mkdir {self.dir_path} && ls -ld {self.dir_path}"
        self.logger.info(mkdir_cmd)
        mkdir_msg = self.userNode.sh(mkdir_cmd).result()
        self.logger.info(mkdir_msg)
        check_dir_cmd = f'''if [ -d {self.dir_path} ]; 
                    then echo "exists"; 
                    else echo "not exists"; fi'''
        self.logger.info(check_dir_cmd)
        mkdir_result = self.userNode.sh(check_dir_cmd).result()
        self.logger.info(mkdir_result)
        self.assertEquals("exists", mkdir_result, '执行失败' + text)
        try:
            text = "-----step2:使用cfe工具注入故障，使/tmp空间磁盘满，" \
               "expect:注入故障成功-----"
            self.logger.info(text)
            cfe_inject_cmd = f'{macro.CFE_PATH}/cfe  ' \
                f'"inject  rfile_full (diskname)  values  (/tmp)" && df /tmp -h'
            self.logger.info(cfe_inject_cmd)
            cfe_inject_res = self.primaryRootNode.sh(cfe_inject_cmd).result()
            self.logger.info(cfe_inject_res)
            self.assertIn(self.constant.CFE_DISK_FULL_SUCCESS_MSG and '100%',
                          cfe_inject_res, '执行失败' + text)

            text = "-----step3:初始用户创建目录对象，expect:创建成功-----"
            self.logger.info(text)
            create_cmd = f"create or replace directory {self.dir_name} " \
                f"as '{self.dir_path}';" \
                f"select * from pg_directory " \
                f"where dirname like '{self.dir_name}';"
            create_msg = self.sh_primary.executDbSql(create_cmd)
            self.logger.info(create_msg)
            self.assertIn(self.constant.create_directory_success_msg,
                          create_msg.splitlines()[0], '执行失败' + text)
            self.assertIn(self.dir_name and '10' and self.dir_path,
                          create_msg.splitlines()[-2], '执行失败' + text)
        finally:
            text = "-----step4:清除故障，expect:清除故障成功-----"
            self.logger.info(text)
            cfe_clean_cmd = f'{macro.CFE_PATH}/cfe "clean ' \
                f'rfile_full where diskname = /tmp" && df /tmp -h'
            self.logger.info(cfe_clean_cmd)
            cfe_clean_res = self.primaryRootNode.sh(cfe_clean_cmd).result()
            self.logger.info(cfe_clean_res)
            self.assertIn(self.constant.CFE_DISK_CLEAN_SUCCESS_MSG,
                          cfe_clean_res, '执行失败' + text)

    def tearDown(self):
        text = "-----step5:删除目录，expect:删除目录成功-----"
        self.logger.info(text)
        drop_cmd = f"drop directory {self.dir_name};"
        drop_msg = self.sh_primary.executDbSql(drop_cmd)
        self.logger.info(drop_msg)
        del_cmd = f"rm -rf {self.dir_path}"
        self.logger.info(del_cmd)
        del_msg = self.userNode.sh(del_cmd).result()
        self.logger.info(del_msg)
        check_dir_cmd = f'''if [ -d {self.dir_path} ]; 
                            then echo "exists"; 
                            else echo "not exists"; fi'''
        self.logger.info(check_dir_cmd)
        del_result = self.userNode.sh(check_dir_cmd).result()
        self.logger.info(del_result)
        self.assertIn(self.constant.drop_directory_success_msg,
                      drop_msg, '执行失败' + text)
        self.assertEquals("not exists", del_result, '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")